﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using SIPO.Library.DataAccess.Report;
using SIPO.Library.DomainObject.Transaction;
using SIPO.Library.DataAccess.Administration.Catalog;
using SIPO.Library.DomainObject.Administration.Catalog;
using SIPO.Library.DataAccess.Transaction;
using SIPO.Web.Support;
using Microsoft.Reporting.WebForms;
using System.Threading.Tasks;
using System.Drawing;

namespace SIPO.Web.lists.Report
{
    public partial class rptPenerimaanlist : BasePage
    {
        recAcceptanceRepo Acceptance = new recAcceptanceRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
        recAcceptanceDetailRepo AcceptanceDetail = new recAcceptanceDetailRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
        recBillingRepo recBillingDal = new recBillingRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
        recDayOffRepo recDayOffDal = new recDayOffRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);


        protected override void OnLoad(EventArgs e)
        {
           
            if (!IsPostBack)
            {
                
             
              
            }
        }
        

        protected void Button1_Click(object sender, EventArgs e)
        {

            string txtPeriode = hfPeriodeKe.Value;
            string parameterTerminDueDate = string.Empty;
            string versi = hfVersi.Value;
            //Ambil Termin Due Date

            List<string> listTermin = recBillingDal.GetTerminDueDate();
            foreach (string terminDate in listTermin)
            {
                //DateTime dateCurrent = Convert.ToDateTime(DateTime.Now.ToString("dd/MM/yyyy"));
                DateTime dateCurrent = Convert.ToDateTime(recDayOffDal.GetCurrentDateFromRecParameter().ToString("dd/MM/yyyy"));
                DateTime dateTermin = Convert.ToDateTime(terminDate + "/" + dateCurrent.ToString("yyyy"));

                if (dateCurrent <= dateTermin)
                {
                    parameterTerminDueDate = " AND recBilling.DueDate <= CONVERT(DATETIME, '" + dateTermin.ToString("dd/MM/yyyy") + "', 103)";
                    break;
                }
            }


            string parameter = string.Empty;
            string parameter2 = string.Empty;
            parameter2 = " and recBilling.BillingDate between " + "CONVERT(DATETIME,'01/01/" + txtPeriode.Substring(6, 4) + "', 103) and CONVERT(DATETIME,'" + txtPeriode + "', 103)";
            int paramdate3 = int.Parse(txtPeriode.Substring(6, 4)) - 1;
            parameter = "= '" + txtPeriode.Substring(6, 4) + "' and transactions.recacceptancedetail.AcceptanceId = (select acceptanceid from transactions.recAcceptance where acceptanceperiod='" + txtPeriode.Substring(6, 4) + "' and versi ='"+ versi +"')";
            string parameter3 = "= '" + paramdate3 + "' and transactions.recacceptancedetail.AcceptanceId = (select acceptanceid from transactions.recAcceptance where acceptanceperiod='" + txtPeriode.Substring(6, 4) + "' and versi ='" + versi + "')";
            string parameter4 = " and recBilling.BillingDate between " + "CONVERT(DATETIME,'01/01/" + paramdate3 + "', 103) and CONVERT(DATETIME,'" + txtPeriode.Replace(txtPeriode.Substring(6, 4),paramdate3.ToString()) + "', 103)";
            
            //string parameter2 = "between " + ddlPeriode0.SelectedValue + " and " + ddlPeriode1.SelectedValue;
            //    string period = "Tahun " + ddlPeriode0.SelectedValue + " s.d " + ddlPeriode1.SelectedValue;
            //    if (ddlPeriode0.SelectedValue == ddlPeriode1.SelectedValue)
            //    {
            //        period = "Tahun " + ddlPeriode0.SelectedValue;
            //    }

            //    if (ddlPeriode0.SelectedValue == ddlPeriode1.SelectedValue)
            //    {
            //        parameter = "= " + ddlPeriode0.SelectedValue +" and Versi=" + ddlVersi.SelectedValue;
            //    }

               
                try
                {
                    DataTable dt = AcceptanceDetail.GetDataReport(parameter,parameter2,parameterTerminDueDate,parameter3,parameter4);

                    rsViewer.Reset();
                    string path = "~/reportviewer/Transaction/rptAcceptance.rdlc";
                    rsViewer.LocalReport.ReportPath = Server.MapPath(path);
                    ReportDataSource rds = new ReportDataSource();
                    rds.Name = "DataSet1";
                    rds.Value = dt;
                    ReportParameter p1 = new ReportParameter("Periode", "Versi "+ versi +" Periode s.d "+ (Convert.ToDateTime(txtPeriode)).ToString("dd MMM yyyy"));
                    ReportParameter p2 = new ReportParameter("PeriodeThn1", txtPeriode.Substring(6, 4));
                    ReportParameter p3 = new ReportParameter("PeriodeThn2", paramdate3.ToString());
                    this.rsViewer.LocalReport.SetParameters(new ReportParameter[] { p1,p2,p3 });

                    rsViewer.LocalReport.DataSources.Clear();
                    rsViewer.LocalReport.DataSources.Add(rds);
                    rsViewer.LocalReport.Refresh();
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                }
            



            
          
            
            
               
            

        }


    }
}